/*
 * B 站直播弹幕抽奖机
 * 
 * @author Zdream
 * @date 2021-01-31
 * @since 0.4.1
 */

function create (context) {
	return {
		template : context.template,
		data : function () {
			return {
        // roomId : this.roomId,
        // any : this.lottery.any,
        // words : this.lottery.words,
        // phase : this.lottery.phase,
        // chosen : this.lottery.chosen,
        // participants : this.lottery.participants.length,

        roomId : null, // 文本框输入数据
        // 状态数据
        status : null,
        words : '',
			}
		},
		props : {
			sid : String,
		},
		methods : {
			onScreenUpdated : function () {
        let status = this.status = context.getStatus(this.sid);
        if (!status || !status.data) {
					return;
				}
				if (status.data) {
          this.roomId = status.data.roomId <= 0 ? this.roomId : status.data.roomId;
          this.words = status.data.words;
				}
			},
			// 事件驱动
			editRoomIdSubmit : function () {
        if (this.roomId == null) {
          return;
        }
				context.sendCommand(this.sid, '/load/room', {roomId : this.roomId});
      },
			editWordsSubmit : function () {
				context.sendCommand(this.sid, '/load/words', {words : this.words});
      },
      btnReset : function () {
        context.sendCommand(this.sid, '/lottery/reset', null);
      },
      btnBegin : function () {
        context.sendCommand(this.sid, '/lottery/begin', null);
      },
      btnEnd : function () {
        context.sendCommand(this.sid, '/lottery/end', null);
      },
      btnReady : function () {
        context.sendCommand(this.sid, '/lottery/ready', null);
      },
      btnPick : function () {
        context.sendCommand(this.sid, '/lottery/pick', null);
      },
		},
		mounted : function () {
			this.onScreenUpdated(); // 更新 chunkWidth
			context.getBus().$on('screen-status-sync-all', this.onScreenUpdated);
			context.getBus().$on('screen-updated', this.onScreenUpdated);
			context.getBus().$on('screen-destroyed', this.onScreenUpdated);
		},
    destroyed : function () {
      context.getBus().$off('screen-status-sync-all', this.onScreenUpdated);
      context.getBus().$off('screen-updated', this.onScreenUpdated);
      context.getBus().$off('screen-destroyed', this.onScreenUpdated);
    }
	};
}

export default create;
